<?php
if (!defined('admin')) {
    exit();
}
checktoken();


//文章来源表
if (!empty(getRequest('articletable'))) {
    $articletable = dbstr(getRequest('articletable'));
    $articletableuri = '&articletable=' . $articletable;
} else {
    $articletableuri = '';
    $articletable = '';
}
if (!admintablecheck($articletable)) {
    adminmsg('', '栏目表名有误');
}

$cid = intval(getRequest('cid'));
$link = adminchannel($cid);
if ($link['ifshowadmin'] == 0) {
    adminmsg('', '此栏目已经禁用');
}
$csetting = json_decode($link['csetting'], 1);
if (isset($csetting['listnodel']) && $csetting['listnodel'] == 1) {
    adminmsg('', '禁止删除');
}//禁止删除
if (empty($articletable)) {
    if (isset($csetting['articletable'])) {
        $articletable = $csetting['articletable'];
    } else {
        die('栏目表名未知');
    }
}
$articletable=tableex($articletable);

if (!power('s', $cid, $power, 3)) {
    adminmsg('', '无权限');
}//用户没有删除权限
$delsql = '';//如果栏目开通了普通用户只能管理自己的文章
if (power('sadmin', 0) || power('s', $cid, $power, 5)) {
} else {
    if (isset($csetting['listadminuid']) && $csetting['listadminuid'] == 1) {
        $delsql = " and adminuid='$myadminuid'";
    }
}

$id = getRequest('id');
if ($id) {
    $ids = explode('_', $id);
    //普通用户只能编辑自己的文章
    getHooks()->do_action('admin_delpost_before');
    foreach ($ids as $value) {
        $value = intval($value);
        $thisid = $value;
        getHooks()->do_action('admin_delpost_loop');
        $query = M()->query("delete from `$articletable` WHERE id='$value' and cid='$cid'$delsql");
        if ($query) {
            $thisid = $value;//把当前value给thisid,方便del函数调用
//            run_admin_hook($cid, 'deleted');
            getHooks()->do_action('admin_delposted');
        }
    }
} else if (getPost('clsall') == '1' && getGet('clsall') == '1') {
    if (power('sadmin', 0) || power('s', $cid, $power, 5)) {
        //删除本栏目所有数据
        $thisid = null;
//        run_admin_hook($cid, 'clsall');
        getHooks()->do_action('admin_clsallpost_before');
//    dump_pre("delete from `$articletable` WHERE cid='$cid'$delsql");
        $query = M()->query("delete from `$articletable` WHERE cid='$cid'$delsql");
        if ($query) {
//            run_admin_hook($cid, 'clsalled');
            getHooks()->do_action('admin_clsallposted');
        }
    } else {
        adminmsg($_SERVER["HTTP_REFERER"], '您没有权限执行此操作！', 3);
    }
} else {
    adminmsg($_SERVER["HTTP_REFERER"], '请求参数不完整！', 1);
}

if (!isset($_SERVER["HTTP_REFERER"])) {
    $_SERVER["HTTP_REFERER"] = '';
}
adminmsg($_SERVER["HTTP_REFERER"], '删除完成', 1);

